package com.soecode.lyf.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;

public class ImageUtils {
	/**
	 * 上传图片
	 * @param request
	 * @param book
	 * @param pictureFile
	 * @throws IOException
	 */
	public static String upload(HttpServletRequest request,
			MultipartFile[] pictureFile) throws IOException {
		String imgPath = null;//装配后的图片地址
		//上传图片
		if(pictureFile!=null&&pictureFile.length>0){
			// 使用UUID给图片重命名，并去掉四个“-”
			for(int i=0;i<pictureFile.length;i++) {
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件的扩展名
			System.out.println("看看"+pictureFile+"大小"+pictureFile[i].getSize()+"字节流"+pictureFile[i].getBytes());
			//String ext = FilenameUtils.getExtension(pictureFile[i].getOriginalFilename());
			//System.out.println("后缀名"+ext);
			// 设置图片上传路径
			//String url = "E:\\tomcat8\\apache-tomcat-8.5.51\\webapps\\docs\\upload\\";
			String url="C:\\Users\\Administrator\\Desktop\\apache-tomcat-8.5.31\\webapps\\docs\\upload\\";
			//String url = request.getSession().getServletContext().getRealPath("/upload");
			System.out.println("tomcat路径保存图片路径"+url);
			// 检验文件夹是否存在
			isFolderExists(url);
			// 以绝对路径保存重名命后的图片
			pictureFile[i].transferTo(new File(url + "/" + name + ".jpg"));
			// 装配图片地址
			if(i==0) {
			imgPath = "http://120.79.178.165:8080/docs/upload/" + name + ".jpg";
			}else {
			imgPath = imgPath + "&" + "http://120.79.178.165:8080/docs/upload/" + name + ".jpg";
			}
			}
		}
		return imgPath;
	}
	/**
	 * 验证文件夹是否存在
	 * @param strFolder
	 * @return
	 */
	public static boolean isFolderExists(String strFolder){
        File file = new File(strFolder);
     
        if (!file.exists()){
           if (file.mkdir()){
               return true;
           }
           else{
               return false;
           }
            
        }
        System.out.println("-----------------文件上传路径："+strFolder);
        return true;
    }
	/**
	 * 获取目录下所有文件(按时间排序)
	 * @param path
	 * @return
	 */
	public static List<File> getFileSort(String path) {
	    List<File> list = getFiles(path, new ArrayList<File>());
	    if (list != null && list.size() > 0) {
	        Collections.sort(list, new Comparator<File>() {
	            public int compare(File file, File newFile) {
	                if (file.lastModified() < newFile.lastModified()) {//降序<;升序>
	                    return 1;
	                } else if (file.lastModified() == newFile.lastModified()) {
	                    return 0;
	                } else {
	                    return -1;
	                }
	            }
	        });
	    }
	    return list;
	}
	/**
	 *  获取目录下所有文件
	 * @param realpath
	 * @param files
	 * @return
	 */
	public static List<File> getFiles(String realpath, List<File> files) {
	    File realFile = new File(realpath);
	    if (realFile.isDirectory()) {
	        File[] subfiles = realFile.listFiles();
	        for (File file : subfiles) {
	            if (file.isDirectory()) {
	                getFiles(file.getAbsolutePath(), files);
	            } else {
	                files.add(file);
	            }
	        }
	    }
	    return files;
	}
}
